package com.kd.crawler.common.utils;

import java.util.LinkedList;
import java.util.Queue;

import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

import com.gargoylesoftware.htmlunit.BrowserVersion;

/**
 * this class uses to a-synchronize ajax request;
 * and reuse phamtonjs
 * @author Administrator
 *
 */
public class WebDriverUtils {
	
	private static final Queue<PhantomJSDriver> pjPool = new LinkedList<PhantomJSDriver>();
	private static final int nDrivers = 1;
	
	static {
		String phantomFileName = "phantomjs-2.1.1";
		String dir = WebDriverUtils.class.getClassLoader().getResource(phantomFileName).getPath();
    	String phantomJsPath = dir + "/bin/phantomjs.exe";
		System.setProperty("phantomjs.binary.path", phantomJsPath);
	}
	public static void init() {
		synchronized(pjPool) {
			if(pjPool.isEmpty()) {
				for(int i=0; i<nDrivers; i++) {
					pjPool.add(new PhantomJSDriver());
				}
			}
		}
	}

	private static PhantomJSDriver getPhantomJSDriver() {
		init();
		PhantomJSDriver pjDriver = null;
		synchronized(pjPool) {
			while(null == pjDriver) {
				pjDriver = pjPool.poll();
				if(null == pjDriver) {
					try {
						pjPool.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return pjDriver;
	}
	private static void onFinish(PhantomJSDriver pjDriver) {
		synchronized(pjPool) {
			pjPool.offer(pjDriver);
			pjPool.notify();
		}
	}
	
	/**
	 * 获取webcollector 自带 htmlUnitDriver实例(模拟默认浏览器)
	 *
	 * @param page
	 * @return
	 */
	public static HtmlUnitDriver getDriver(String url) {
		HtmlUnitDriver driver = new HtmlUnitDriver();
		driver.setJavascriptEnabled(true);
		driver.get(url);
		return driver;
	}

	/**
	 * 获取webcollector 自带htmlUnitDriver实例 
	 *
	 * @param page
	 * @param browserVersion 模拟浏览器
	 * @return
	 */
	public static HtmlUnitDriver getDriver(String url, BrowserVersion browserVersion) {
		HtmlUnitDriver driver = new HtmlUnitDriver(browserVersion);
		driver.setJavascriptEnabled(true);
		driver.get(url);
		return driver;
	}
	
	/**
	 * according to the given url, return back a html.
	 * But some time fetch failed, so html is ''.
	 * @param url
	 * @return
	 */
	public static String getHtml(String url) {
		PhantomJSDriver driver = getPhantomJSDriver();
    	driver.get(url);
    	//here, for returning right result at most, waiting for some seconds is always worthy.
    	try {
    		System.out.println("wait for 5s");
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
//    	JavascriptExecutor js = (JavascriptExecutor) driver;
//    	js.executeScript("function(){}");
    	String html = driver.getPageSource();
//    	response.setStatus();
    	onFinish(driver);
    	return html;
	}
	
	public static void main(String[] args) {
		String url = "http://baoliao.cq.qq.com/pc/detail.html?id=294064";
		System.out.println("start....");
		System.out.println(getHtml(url));
//		url = "http://baoliao.cq.qq.com/pc/detail.html?id=294064";
//		System.out.println(getResponse(url));
		System.out.println("end");
	}
}
